# Copyright 2022 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//bazel:yacl.bzl", "OMP_CFLAGS", "OMP_DEPS", "OMP_LINKFLAGS", "yacl_cc_binary", "yacl_cc_library", "yacl_cc_test")

package(default_visibility = ["//visibility:public"])

yacl_cc_library(
    name = "hamming",
    hdrs = ["hamming.h"],
    deps = [
        "//yacl/base:exception",
        "//yacl/base:int128",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/types:span",
    ],
)

yacl_cc_test(
    name = "hamming_test",
    srcs = ["hamming_test.cc"],
    deps = [
        ":hamming",
    ],
)

yacl_cc_library(
    name = "segment_tree",
    hdrs = ["segment_tree.h"],
)

yacl_cc_test(
    name = "segment_tree_test",
    srcs = ["segment_tree_test.cc"],
    deps = [
        ":segment_tree",
    ],
)

yacl_cc_library(
    name = "scope_guard",
    hdrs = ["scope_guard.h"],
    deps = ["@com_google_absl//absl/cleanup"],
)

yacl_cc_test(
    name = "scope_guard_test",
    srcs = ["scope_guard_test.cc"],
    deps = [
        ":scope_guard",
    ],
)

yacl_cc_library(
    name = "parallel",
    srcs = [
        "parallel.cc",
    ],
    hdrs = [
        "parallel.h",
    ],
    deps = [
        ":thread_pool",
    ],
)

# deprecated, use serializer instead
yacl_cc_library(
    name = "serialize",
    hdrs = ["serialize.h"],
    deps = [
        ":serializer",
    ],
)

yacl_cc_library(
    name = "serializer",
    hdrs = [
        "serializer.h",
        "serializer_adapter.h",
    ],
    deps = [
        "@com_github_msgpack_msgpack//:msgpack",
        "@yacl//yacl/base:byte_container_view",
        "@yacl//yacl/base:int128",
        "@yacl//yacl/io/msgpack:buffer",
    ],
)

yacl_cc_test(
    name = "serializer_test",
    srcs = ["serializer_test.cc"],
    deps = [
        ":serializer",
    ],
)

yacl_cc_library(
    name = "elapsed_timer",
    srcs = ["elapsed_timer.cc"],
    hdrs = ["elapsed_timer.h"],
)

yacl_cc_test(
    name = "elapsed_timer_test",
    srcs = ["elapsed_timer_test.cc"],
    deps = [
        ":elapsed_timer",
    ],
)

yacl_cc_library(
    name = "hash_combine",
    hdrs = ["hash_combine.h"],
)

yacl_cc_library(
    name = "matrix_utils",
    srcs = ["matrix_utils.cc"],
    hdrs = [
        "matrix_utils.h",
    ],
    copts = select({
        "@platforms//cpu:aarch64": [
        ],
        "//conditions:default": [
            "-march=haswell",
            "-mavx2",
        ],
    }),
    defines = select({
        "@platforms//cpu:aarch64": [
        ],
        "//conditions:default": [
            "YACL_ENABLE_BMI2",
        ],
    }),
    deps = [
        "//yacl/base:block",
        "//yacl/base:byte_container_view",
        "//yacl/base:int128",
        "@com_github_google_cpu_features//:cpu_features",
        "@com_google_absl//absl/types:span",
    ] + select({
        "@platforms//cpu:aarch64": [
            "@com_github_dltcollab_sse2neon//:sse2neon",
        ],
        "//conditions:default": [],
    }),
)

yacl_cc_test(
    name = "matrix_transpose_test",
    srcs = ["matrix_transpose_test.cc"],
    deps = [
        ":matrix_utils",
    ],
)

yacl_cc_binary(
    name = "matrix_transpose_bench",
    srcs = ["matrix_transpose_bench.cc"],
    deps = [
        ":matrix_utils",
        "//yacl/crypto/tools:prg",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

yacl_cc_binary(
    name = "matrix_uint128_bench",
    srcs = ["matrix_uint128_bench.cc"],
    deps = [
        ":matrix_utils",
        "//yacl/crypto/tools:prg",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

yacl_cc_library(
    name = "cuckoo_index",
    srcs = ["cuckoo_index.cc"],
    hdrs = ["cuckoo_index.h"],
    linkopts = ["-lm"],
    deps = [
        "//yacl/base:exception",
        "//yacl/base:int128",
        "@com_google_absl//absl/types:span",
    ],
)

yacl_cc_test(
    name = "cuckoo_index_test",
    srcs = ["cuckoo_index_test.cc"],
    deps = [
        ":cuckoo_index",
        "//yacl/crypto/rand",
    ],
)

yacl_cc_library(
    name = "platform_utils",
    srcs = ["platform_utils.cc"],
    hdrs = ["platform_utils.h"],
    copts = select({
        "@platforms//cpu:aarch64": [""],
        "//conditions:default": [
            "-mbmi2",
        ],
    }),
    deps = select({
        "@platforms//cpu:x86_64": [
            "@com_github_google_cpu_features//:cpu_features",
        ],
        "//conditions:default": [],
    }),
)

yacl_cc_test(
    name = "platform_utils_test",
    srcs = ["platform_utils_test.cc"],
    linkstatic = True,
    deps = [
        ":platform_utils",
    ],
)

yacl_cc_test(
    name = "parallel_test",
    srcs = ["parallel_test.cc"],
    deps = [
        ":parallel",
        "//yacl/base:exception",
    ],
)

yacl_cc_binary(
    name = "parallel_bench",
    srcs = ["parallel_bench.cc"],
    copts = OMP_CFLAGS,
    linkopts = OMP_LINKFLAGS,
    deps = [
        ":parallel",
        "@com_github_google_benchmark//:benchmark",
    ] + OMP_DEPS,
)

yacl_cc_library(
    name = "thread_pool",
    srcs = ["thread_pool.cc"],
    hdrs = ["thread_pool.h"],
    deps = [
        "//yacl/base:exception",
    ],
)

yacl_cc_library(
    name = "compile_time_utils",
    hdrs = [
        "compile_time_utils.h",
    ],
)
